package managedbean.SCM.materialRequest;

import entity.SCM.MaterialLine;
import entity.RawMaterial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.SCM.BatchBeanLocal;
import session.stateless.SCM.MaterialRequestBeanLocal;

@ManagedBean(name = "addMaterialRequestManagerBean")
@ViewScoped
public class AddMaterialRequestManagerBean implements Serializable{

    public AddMaterialRequestManagerBean() {
    }
    @EJB
    private BatchBeanLocal batchBeanLocal;
    @EJB
    private MaterialRequestBeanLocal materialRequestBeanLocal;
    private String materialType;
    private Long quantity;
    private String statusMessage;
    private List<String> materialTypes = new ArrayList<String>();     
    private List<MaterialLine> tempMaterialLines = new ArrayList<MaterialLine>();

    public String getMaterialType() {
        return materialType;
    }

    public void setMaterialType(String materialType) {
        this.materialType = materialType;
    }

    public Long getQuantity() {
        return quantity;
    }

    public void setQuantity(Long quantity) {
        this.quantity = quantity;
    }

    public List<MaterialLine> getTempMaterialLines() {
        return tempMaterialLines;
    }

    public void setTempMaterialLines(List<MaterialLine> tempMaterialLines) {
        this.tempMaterialLines = tempMaterialLines;
    }

    public List<String> getMaterialTypes() {
        return materialTypes;
    }

    public void setMaterialTypes(List<String> materialTypes) {
        this.materialTypes = materialTypes;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public List<String> getDisplayMaterialTypes(){
        materialTypes = batchBeanLocal.getMaterialType();
        return materialTypes;
    }
    
    public void displayTempList(ActionEvent event) throws Exception{
    
        RawMaterial rawMaterial = batchBeanLocal.findRawMaterial(materialType);
        MaterialLine materialLine = new MaterialLine();
        materialLine.setRawMaterial(rawMaterial);
        materialLine.setQuantity(quantity);
        tempMaterialLines.add(materialLine);
    }
    
    public void saveMaterialRequest(ActionEvent event) throws Exception {

        List<MaterialLine> materialLineList = new ArrayList<MaterialLine>();
        MaterialLine materialLine = new MaterialLine();
        for (int i = 0; i < tempMaterialLines.size(); i++) {
            if (tempMaterialLines.get(i).getQuantity() != null && tempMaterialLines.get(i).getQuantity().longValue() > 0) {
                materialLine = materialRequestBeanLocal.createMaterialLine(tempMaterialLines.get(i).getRawMaterial().getMaterialType(), tempMaterialLines.get(i).getQuantity());
                materialLineList.add(materialLine);
            }
        }

        Long id = materialRequestBeanLocal.createMaterialRequest(materialLineList);

        if (id == null) {
            statusMessage = "Please Try Again.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful:" + statusMessage + "", ""));
        } else {
            statusMessage = "New Material Request " + id + " Saved Successfully.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));

        }
    }
    
}
